/*
 * $Id: ActRequestListDAO.java,v 1.10 2009/10/14 22:53:08 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.database.dao;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.exceptions.AlreadyCleanedUpException;
import com.ge.healthcare.autosc.common.database.pojo.ActRequest;
import com.ge.healthcare.autosc.common.database.pojo.ActRequestList;

/**
 * @author 212042764
 * 
 */
public class ActRequestListDAO extends BaseDAO implements IRequestListDAO {

	/**
	 * Eclipse genearted
	 */
	private static final long serialVersionUID = 7616061073644432616L;

	public ActRequestListDAO() {
		super(ActRequestList.TABLE_NAME);
	}

	public ActRequestList getRequestListByID(Long requestListId) {
		Object requestList = findObjectByID(ActRequestList.COL_REQUEST_ID,
				requestListId);
		if (!ASCUtil.isNullOrEmptyObject(requestList)) {
			return (ActRequestList) requestList;
		}
		return null;
	}

	public Long getNewBatchId() {
		ASCLogger.debug(this.getClass(), "getNewBatchId", "get new batch Id");
		return (Long) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException {
				return (Long) session
						.createSQLQuery(
								"select act_request_list_batch_id_seq.nextVal as id from dual")
						.addScalar("id", Hibernate.LONG).uniqueResult();
			}
		});
	}
	public void completeRequestList(ActRequest request, Long passCount,
			Long failCount) {
		String mName = "completeRequestList";
		StringBuffer msg = new StringBuffer();
		if( ASCUtil.isNullOrEmptyObject(request)){
			msg.append( "Request passed was null");
			ASCLogger.error(this.getClass(), mName, msg);
		} else if( ASCUtil.isNullOrEmptyObject(request.getRequestListId())){
			msg.append( "Request passed has null request list id: ");
			msg.append( request);
			ASCLogger.error(this.getClass(), mName, msg);
		} else {
			msg.append( "Updating Request List: ").append( request.getRequestListId());
			msg.append( " passCount: ").append( passCount);
			msg.append( " failCount: ").append( failCount);
			
			StringBuffer query = new StringBuffer("UPDATE ");
			query.append(ActRequestList.TABLE_NAME);
			query.append(" set ");
			query.append( ActRequestList.COL_PASS_COUNT).append( " = :");
			query.append( ActRequestList.COL_PASS_COUNT);
			query.append( " , ");
			query.append( ActRequestList.COL_FAIL_COUNT).append( " = :");
			query.append( ActRequestList.COL_FAIL_COUNT);
			query.append( " where ");
			query.append(ActRequestList.COL_REQUEST_ID).append(" = :");
			query.append(ActRequestList.COL_REQUEST_ID);
			query.append( " and ( " );
			query.append( ActRequestList.COL_PASS_COUNT).append( " IS NULL ");
			query.append( " or " );
			query.append( ActRequestList.COL_PASS_COUNT).append( " = 0 ");
			query.append( " ) and (" );
			query.append( ActRequestList.COL_FAIL_COUNT).append( " IS NULL ");
			query.append( " or  " );
			query.append( ActRequestList.COL_FAIL_COUNT).append( " = 0 ");
			query.append( " )");
			msg.append(" query: ").append(query);
	
			Session s = getHibernateTemplate().getSessionFactory().getCurrentSession();
			int updateCount = 0;
			try {
				updateCount = s.createQuery(query.toString())
					.setLong( ActRequestList.COL_PASS_COUNT, passCount )
					.setLong( ActRequestList.COL_FAIL_COUNT, failCount )
					.setLong( ActRequestList.COL_REQUEST_ID, request.getRequestListId() )
					.executeUpdate();
				msg.append(" Updated: ").append(updateCount);
				if( updateCount < 1 ){
					msg.append( " No Rows updated - Request List already sent for cleanup " );
					throw new AlreadyCleanedUpException( msg.toString());
				}
			} catch (AlreadyCleanedUpException e) {
				ASCLogger.info(this.getClass(), mName, msg);
				throw e;
			} catch (RuntimeException e) {
				msg.append(" Delete Failed: ").append(e.getMessage());
				ASCLogger.error(this.getClass(), mName, msg);
				throw e;
			} finally {
				//s.close();
			}
			ASCLogger.info(this.getClass(),mName, msg);
		}
	}
	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#save(java.lang.Object)
	 */
	public void save(Object pojo) {
		super.save(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.IRequestTypeDAO#saveAlways(java.lang.Object)
	 */
	public void saveAlways(Object pojo) {
		super.save(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#deleteByID(java.lang.Object)
	 */
	public void deleteByID(Object pojo) {
		super.deleteByID(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#refresh(java.lang.Object)
	 */
	public void refresh(Object pojo) {
		super.refresh(pojo);
	}

	
}
